package com.cloud.hotline.api.provider;

import java.util.Date;

import com.cloud.hotline.api.bean.HotlineCalculateForm;
import com.cloud.hotline.api.consts.HotLineConsts;
import com.cloud.hotline.common.utils.DateHelper;

public class HotlineStreetCalculateProvider {
	private static final String SELECT = "select @rownum:=@rownum+1 AS pm, unit_name as jdmc, sum(c) as jrzs, "
			+ "sum(sjzp) as jrszps, sum(qjzp) as jrqzps from (select (SELECT @rownum:=0) r, unit_name, 1 as c, "
			+ "if(dispatch_type='市级直派', 1, 0) as sjzp,  if(dispatch_type='区级转派', 1, 0) as qjzp from to_hotline_record where delete_flag = 0 ";

	private static final String GROUP = ") t group by unit_name";

	public String caculate(HotlineCalculateForm form) {
		StringBuilder sqlBuilder = new StringBuilder(SELECT);
		// 考核周期：当天
		Date now = new Date();
		sqlBuilder.append(" and state_date >= '" + DateHelper.dateTimeToStr(DateHelper.getDateBegin(now)) + "'");
		sqlBuilder.append(" and state_date <= '" + DateHelper.dateTimeToStr(DateHelper.getDateEnd(now)) + "'");
		// 数据类型
		sqlBuilder.append(" and data_type_id = " + HotLineConsts.DATA_TYPE_STREET_UNDERTAKE);
		sqlBuilder.append(GROUP);
		return sqlBuilder.toString();
	}
}
